import { exec } from 'child_process';

class LinuxLocker {
    private isLocked: boolean;

    constructor() {
        this.isLocked = false;
    }

    async enableLock(): Promise<void> {
        if (this.isLocked) return;
        console.log('🐧 启用 Linux 系统锁定');

        try {
            const commands: string[] = [
                // 禁用 Alt+Tab
                'gsettings set org.gnome.desktop.wm.keybindings switch-applications "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings switch-applications-backward "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings switch-windows "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings switch-windows-backward "[]"',

                // 禁用工作区切换
                'gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-left "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-right "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-up "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-down "[]"',

                // 禁用窗口管理
                'gsettings set org.gnome.desktop.wm.keybindings minimize "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings maximize "[]"',
                'gsettings set org.gnome.desktop.wm.keybindings close "[]"',

                // 禁用系统快捷键
                'gsettings set org.gnome.settings-daemon.plugins.media-keys logout "[]"',
                'gsettings set org.gnome.settings-daemon.plugins.media-keys screensaver "[]"',
                'gsettings set org.gnome.settings-daemon.plugins.media-keys home "[]"',
                'gsettings set org.gnome.settings-daemon.plugins.media-keys www "[]"',

                // Deepin 特定设置
                'gsettings set com.deepin.wm keybindings switch-to-workspace-left "[]"',
                'gsettings set com.deepin.wm keybindings switch-to-workspace-right "[]"',
                'gsettings set com.deepin.wm keybindings switch-to-workspace-up "[]"',
                'gsettings set com.deepin.wm keybindings switch-to-workspace-down "[]"',

                // 禁用鼠标右键
                'xmodmap -e "pointer = 1 0 3 4 5 6 7 8 9 10"',

                // 禁用任务栏和启动器
                'dde-dock --hide-mode smart',
                'dde-launcher --hide',

                // 禁用桌面图标
                'gsettings set com.deepin.dde.desktop show-desktop-icons false'
            ];

            for (const cmd of commands) {
                await this.executeCommand(cmd);
            }

            this.isLocked = true;
            console.log('✅ Linux 系统锁定已启用');
        } catch (error) {
            console.error('❌ Linux 锁定失败:', error);
        }
    }

    async disableLock(): Promise<void> {
        if (!this.isLocked) return;
        console.log('🐧 禁用 Linux 系统锁定');

        try {
            const commands: string[] = [
                // 恢复默认设置
                'gsettings reset org.gnome.desktop.wm.keybindings switch-applications',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-applications-backward',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-windows',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-windows-backward',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-to-workspace-left',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-to-workspace-right',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-to-workspace-up',
                'gsettings reset org.gnome.desktop.wm.keybindings switch-to-workspace-down',
                'gsettings reset org.gnome.desktop.wm.keybindings minimize',
                'gsettings reset org.gnome.desktop.wm.keybindings maximize',
                'gsettings reset org.gnome.desktop.wm.keybindings close',
                'gsettings reset org.gnome.settings-daemon.plugins.media-keys logout',
                'gsettings reset org.gnome.settings-daemon.plugins.media-keys screensaver',
                'gsettings reset org.gnome.settings-daemon.plugins.media-keys home',
                'gsettings reset org.gnome.settings-daemon.plugins.media-keys www',
                'gsettings reset com.deepin.wm keybindings switch-to-workspace-left',
                'gsettings reset com.deepin.wm keybindings switch-to-workspace-right',
                'gsettings reset com.deepin.wm keybindings switch-to-workspace-up',
                'gsettings reset com.deepin.wm keybindings switch-to-workspace-down',

                // 恢复鼠标右键
                'xmodmap -e "pointer = 1 2 3 4 5 6 7 8 9 10"',

                // 恢复任务栏和启动器
                'dde-dock --hide-mode keep-showing',
                'dde-launcher --show',

                // 恢复桌面图标
                'gsettings set com.deepin.dde.desktop show-desktop-icons true'
            ];

            for (const cmd of commands) {
                await this.executeCommand(cmd);
            }

            this.isLocked = false;
            console.log('✅ Linux 系统锁定已禁用');
        } catch (error) {
            console.error('❌ Linux 解锁失败:', error);
        }
    }

    private executeCommand(command: string): Promise<void> {
        return new Promise((resolve) => {
            exec(command, (error) => {
                if (error) {
                    console.log(`命令执行警告: ${command} - ${error.message}`);
                }
                resolve();
            });
        });
    }
}

export default LinuxLocker;